import {Injectable} from '@angular/core';
import {HttpService} from '@services/http.service';
import * as Util from 'util';

@Injectable({
    providedIn: 'root'
})
export class InterfaceService {

    constructor(private http: HttpService) {
    }

    login(username, password) {
        return new Promise((resolve, reject) => {
            this.http.post('/user/login.do', {json: {CZY01: username, CZY02: password}})
                .then(v => {
                    if (v.data.STATE == 1) {
                        let userInfo = v.data.userInfo;
                        let sessionid = v.data.sessionid;

                        let userData = {};
                        userData['PCRM_CZY01'] = userInfo['USERID'];
                        userData['PCRM_CZY02'] = userInfo['CZY01'];
                        userData['PCRM_CZY03'] = userInfo['CZY03'];
                        userData['PCRM_CZY06'] = userInfo['CZY06'];
                        userData['PCRM_GW01'] = userInfo['GW01'];
                        userData['PCRM_GW02'] = userInfo['GW02'];
                        userData['PCRM_BM01'] = userInfo['BM01'];
                        userData['PCRM_BM02'] = userInfo['BM02'];
                        userData['PCRM_BM03'] = userInfo['BM03'];
                        userData['PCRM_BM_BM01'] = userInfo['BM_BM01'];
                        userData['PCRM_GSXX01'] = userInfo['GSXX01'];

                        userData['SCM_BM01'] = userInfo['SCM_BM01'];
                        userData['SCM_RYXX08'] = userInfo['RYXX08'] == null ? [] : userInfo['RYXX08'].split(';');
                        userData['SCM_RYXX23'] = userInfo['RYXX23'];
                        userData['SCM_JSSY01'] = userInfo['JSSY01'];
                        userData['SCM_BBBZ01'] = userInfo['BBBZ01'];
                        userData['SCM_BZ04'] = userInfo['BZ04'];
                        userData['PCRM_WLDW01'] = userInfo['WLDW01'];
                        userData['PCRM_CZY10'] = userInfo['CZY10'];
                        userData['FORM_MRCD'] = userInfo['MRCD']; // 默认菜单
                        userData['FORM_DCBJ'] = userInfo['DCBJ']; // 导出标记
                        userData['skbj'] = userInfo['SKBJ']; // 受控标记
                        userData['SessionID'] = userInfo['SessionID'];

                        userData['PersonID'] = userInfo['CZY01'];
                        userData['CompanyID'] = userInfo['GSXX01'];

                        localStorage.setItem('userInfo', JSON.stringify(userData));
                        localStorage.setItem('SessionID', userInfo['SessionID']);

                        resolve(userData);
                    } else {
                        reject('账号密码错误');
                    }
                })
                .catch(e => {
                    reject(e);
                });
        });
    }

    searchStartOrder(data) {
        return this.http.api('app.engineer.searchStartOrder', data);
    }
}
